package light.mvc.service.admin.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.admin.Tresource;
import light.mvc.model.admin.Trole;
import light.mvc.page.admin.Role;
import light.mvc.page.base.DropDownBox;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.Tree;
import light.mvc.service.admin.RoleServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl implements RoleServiceI {

	@Autowired
	private BaseDaoI<Trole> roleDao;

	@Autowired
	private BaseDaoI<Tresource> resourceDao;

	@Override
	public void add(Role role) {
		Trole t = new Trole();
		BeanUtils.copyProperties(role, t);
		roleDao.save(t);
	}

	@Override
	public void delete(Integer id) {
		Trole t = roleDao.get(Trole.class, id);
		roleDao.delete(t);
	}

	@Override
	public void edit(Role role) {
		Trole t = roleDao.get(Trole.class, role.getId());
		BeanUtils.copyProperties(role, t);
		roleDao.update(t);
	}

	@Override
	public Role get(Integer id) {
		Trole t = roleDao.get(Trole.class, id);
		Role role = new Role();
		BeanUtils.copyProperties(t, role);
		
		Set<Tresource> resources = t.getResources();
		boolean b = false;
		String ids = "";
		String names = "";
		for (Tresource resource : resources) {
			if (b) {
				ids += ",";
				names += ",";
			} else {
				b = true;
			}
			ids += resource.getId();
			names += resource.getName();
		}
		role.setResourceIds(ids);
		role.setResourceNames(names);
		return role;
	}
	
	@Override
	public List<Role> dataGrid(Role roleSome, PageFilter ph) {
		List<Role> roles = new ArrayList<Role>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Trole t ";
		List<Trole> list = roleDao.find(hql + whereHql(roleSome,params) + orderHql(ph), params,ph.getPage(), ph.getRows());
		for (Trole t : list) {
			Role role = new Role();
			BeanUtils.copyProperties(t, role);
			roles.add(role);
		}
		return roles;
	}

	@Override
	public Long count(Role role, PageFilter ph) {
		String hql = " from Trole t ";
		return roleDao.count("select count(*) " + hql);
	}

	@Override
	public void grant(Role role) {
		Trole t = roleDao.get(Trole.class, role.getId());
		Set<Tresource> resources = new HashSet<Tresource>(0);
		if ((role.getResourceIds() != null) && !role.getResourceIds().equalsIgnoreCase("")) {
			for (String id : role.getResourceIds().split(",")) {
				Tresource resource = resourceDao.get(Tresource.class,Long.valueOf(id));
				resources.add(resource);
			}
		} else {
			t.setResources(null);
		}
		
		t.setResources(resources);
		roleDao.update(t);
	}

	@Override
	public List<Tree> tree() {
		List<Trole> list = null;
		List<Tree> trees = new ArrayList<Tree>();

		list = roleDao.find("select t from Trole t order by t.id");

		if ((list != null) && (list.size() > 0)) {
			for (Trole r : list) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				tree.setText(r.getName());
				tree.setIconCls("icon_role");
				trees.add(tree);
			}
		}
		return trees;
	}
	
	private String whereHql(Role role, Map<String, Object> params) {
		String hql = "";
		if (role != null) {
			hql += " where 1=1 ";
			if (role.getName() != null) {
				hql += " and t.name like :name";
				params.put("name", "%" + role.getName() + "%");
			}
		}
		return hql;
	}
	
	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	@Override
	public List<DropDownBox> listName() {
		List<DropDownBox> dropDownList = new ArrayList<DropDownBox>();
		List<Trole> list = roleDao.find("select t from Trole t order by t.id");
		if ((list != null) && (list.size() > 0)) {
			for (Trole t : list) {
				DropDownBox box = new DropDownBox();
				box.setId(t.getId().toString());
				box.setText(t.getName());
				dropDownList.add(box);
			}
		}
		return dropDownList;
	}
}
